// pages/home/scan/scan.js
const app = getApp()
var base_url = app.globalData.url
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.getSystemInfo({

      success: function (res) {
        let clientHeight = res.windowHeight;
        let clientWidth = res.windowWidth;
        let ratio = 750 / clientWidth;
        let height = clientHeight * ratio;
        that.setData({
          height: clientHeight,
          width: clientWidth,
          iwidth: (clientWidth - 200) / 2
        });
      }
    });

    setInterval(function () {
      that.daojishi();
      that.ctx = wx.createCameraContext()
    }, 10000)



  },

  daojishi: function () {
    var that = this;
    if (!that.data.counting) {
      //开始倒计时5秒
      countDown(that, 1);
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.animation = wx.createAnimation({
      // duration: 100,
      timingFunction: 'ease-in-out',
      delay: 10,
      transformOrigin: "50% 50%",
      duration: 1000,
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let m = true;
    setInterval(function () {
      if (m) {
        this.animation.translateY(250).step({ duration: 3000 })
        m = !m;
      } else {
        this.animation.translateY(-10).step({ duration: 3000 })
        m = !m;
      }

      // this.animation.height('100px')
      this.setData({  //输出动画  
        animation: this.animation.export()
      })
    }.bind(this), 3000)

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

function countDown(that, count) {
  if (count == 0) {
    //等于0时拍照 
    that.ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        console.log(res.tempImagePath)
        // wx.getFileSystemManager().readFile({
        //   filePath: res.tempImagePath, //选择图片返回的相对路径
        //   encoding: 'base64', //编码格式
        //   success: res => { //成功的回调
        //     console.log('data:image/png;base64,' + res.data)
        //     wx.request({
        //       url: base_url + '/scanLearn/',
        //       method: 'POST',
        //       data: {
        //         x: that.data.width / 2 - 100,
        //         y: 100,
        //         w: 200,
        //         h: 200,
        //         file: res.data
        //       },
        //       header: {
        //         "Content-Type": "application/x-www-form-urlencoded"
        //       },
        //       success: function (res) {
        //         if (res.data['flag'] == true) {
        //           console.log(1)
        //         }
        //       }
        //     })
        //   }
        // })
        wx.getImageInfo({
          src: res.tempImagePath,
          success: function (res) {
            
            var bili_w = res.width/that.data.width
            var bili_h = res.height/that.data.height
            var px = that.data.iwidth + 70
            var py = 170
            var ix = bili_w*px
            var iy = bili_h*py
            var w = bili_w*60
            var h = bili_h * 60

            that.setData({
              ix: ix,
              iy: iy,
              w:w,
              h:h
            })

          },
          fail:function(res){
            console.log(res)
          }
        })

        wx.uploadFile({
          url: base_url + "/scanLearn/",
          filePath: res.tempImagePath,
          name: 'file',
          formData: {
            x: that.data.iwidth,
            y: 100,
            w: 200,
            wMax: that.data.width
          },
          success: function (res) {
            // console.log(res)
            var jsonObj = JSON.parse(res.data)
            if (jsonObj['flag'] == true) {
              // console.log(jsonObj['words_lst'])
              wx.setStorage({
                key: 'searchwordDatas',
                data: jsonObj['words_lst'],
                success: function (res) {
                  wx.redirectTo({
                    url: '../searchword/searchword',
                    success: function (res) {
                      console.log(res)
                    },
                    fail: function () { }
                  })
                }
              })
            }

          }
        })
      }
    })
    that.setData({
      counting: false
    })
    return;
  }


  setTimeout(function () {
    wx.hideLoading()
  }, 1000)
  that.setData({
    counting: true,
  })
  setTimeout(function () {
    count--;
    countDown(that, count);
  }, 3000);
}
